package com.itsxb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itsxb.constants.Constants;
import com.itsxb.constants.GlobalExceptionCode;
import com.itsxb.feign.LoginApi;
import com.itsxb.pojo.domain.Customer;
import com.itsxb.mapper.CustomerMapper;
import com.itsxb.pojo.dto.CustomerDto;
import com.itsxb.pojo.dto.LoginDto;
import com.itsxb.result.R;
import com.itsxb.service.ICustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itsxb.service.ICustomerSummaryService;
import com.itsxb.service.ICustomerWalletService;
import com.itsxb.template.WechatTemplate;
import com.itsxb.utils.AssertUtil;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 客户 服务实现类
 * </p>
 *
 * @author 奇天书
 * @since 2023-12-21
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {

    @Autowired
    private WechatTemplate wechatTemplate;
    @Autowired
    private ICustomerSummaryService customerSummaryService;
    @Autowired
    private ICustomerWalletService customerWalletService;
    @Autowired
    private LoginApi loginApi;
//    @GlobalTransactional(name = "customer-wechat-register",rollbackFor = Exception.class)
    @Override
    public void wechatRegistory(CustomerDto customerDto) {
        //service层业务逻辑层 他完成三部分
        //1.JSR303检验
        //1.1JSR303 校验前端给后端的数据
        //2.业务校验

        //2.2 判断
        //3.业务逻辑实现
        //3.1通过自己封装的starter获取openId
        String openId = wechatTemplate.code2OpenId(customerDto.getLoginCode());
        //2.1 判断openid是否为空
        AssertUtil.isNotNull(openId, GlobalExceptionCode.WECHAT_REGISTER_ERROR);
        Customer customer = super.getOne(new LambdaQueryWrapper<Customer>().eq(Customer::getOpenId, openId));
        //2.3 业务校验
        AssertUtil.isNull(customer,GlobalExceptionCode.PARAM_PHONE_EXIST);
        //3.2 获取phoneNumber 手机号
        String phoneNumber = wechatTemplate.code2Phone(customerDto.getPhoneCode());
        //3.3
        Long id = saveCustomer(openId,phoneNumber);
        customerSummaryService.initCustomerSummary(id);

        customerWalletService.initCustomerWallet(id);

        LoginDto loginDto = new LoginDto();
        loginDto.setId(id);
        loginDto.setType(Constants.Login.TYPE_CUSTOMER);
        loginDto.setOpenId(openId);
        loginDto.setPhone(phoneNumber);
        R r = loginApi.saveLogin(loginDto);
//        int y = 1/0;
        AssertUtil.isNotNull(r,GlobalExceptionCode.WECHAT_REGISTER_ERROR);
    }



    private Long saveCustomer(String openId,String phoneNumber) {
        Customer customer = new Customer();
        //这里的ID从哪里拿的
        customer.setBitState(0L);
        customer.setPhone(phoneNumber);
        customer.setCreateTime(new Date());
        customer.setOpenId(openId);
        //
        super.save(customer);
        return customer.getId();
    }

}
